package china.accountinghelper.activity;

import com.xdroid.database.SQLiteOpenAssistant;

import china.wc.tallyhelper.R;
import china.wc.tallyhelper.adapter.AdapterGridViewIndexMenu;
import china.wc.tallyhelper.controls.SlideMenu;
import china.wc.tallyhelper.controls.SlideMenuItem;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class ActivityMain extends ActivityFrame implements OnItemClickListener
{
    public static int DIALOG_ABOUT = 1;
    
    private GridView                 mFramebodyMain_GridView_IndexMenu;
    private AdapterGridViewIndexMenu mAdapterIndexMenu;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 载入主页面Layout文件
        loadFramebody(R.layout.framebody_main);
        // 生成主界面的SlideMenu菜单
        creatSlideMenu(R.array.slidemenu_index);
        // 设置标题
        setTopTitle(R.string.app_name);
        
        // 如果数据库不存在，打开欢迎界面
        if (!databaseExist()) {
            openActivity(ActivityWelcom.class);
        }

        executeInitTask(null);        
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle args)
    {
        AlertDialog.Builder _Builder = new AlertDialog.Builder(this);
        
        if (id == DIALOG_ABOUT) {
            try {
                _Builder.setTitle("关于本软件")
                    .setMessage("软件版本："+getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName);
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        
        return _Builder.create();
    }

    // SlideMenu的点击事件
    @Override
    public void onSlideMenuItemClick(SlideMenu pSlideMenu, SlideMenuItem pSlideMenuItem, AdapterView<?> pParent, View pView, int pPosition, long pId)
    {
        if (pPosition == 0) {
            slideMenuToggle();
            openActivity(ActivityWelcom.class);
        } else if (pPosition == 1) {
            slideMenuToggle();
            Intent _Data=new Intent(Intent.ACTION_SENDTO);  
            _Data.setData(Uri.parse("mailto:dna1982@gmail.com"));  
            startActivity(_Data);
        } else if (pPosition == 2) {
            slideMenuToggle();
            showDialog(DIALOG_ABOUT, null);
        }
        
    }

    // 主页菜单的点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        if (position == 0) {
            openActivity(ActivityExpenseAddAndEdit.class);
        } else if (position == 1) {
            openActivity(ActivityStatistics.class);
        } else if (position == 2) {
            openActivity(ActivityBook.class);
        } else if (position == 3) {
            openActivity(ActivityPerson.class);
        } else if (position == 4) {
            openActivity(ActivityCategory.class);
        }
    
    }

    @Override
    protected void initFields()
    {
        mAdapterIndexMenu = new AdapterGridViewIndexMenu(this);
        
        // 如果数据库不存在，打开关闭一次数据库，以便数据库进行初始化。
        if (!databaseExist()) {
            SQLiteDatabase db=SQLiteOpenAssistant.getInstance().getWritableDatabase();
            db.close();
        }
    }

    @Override
    protected void initViews()
    {
        mFramebodyMain_GridView_IndexMenu = (GridView) findViewById(R.id.FramebodyMain_GridView_IndexMenu);
    }

    @Override
    protected void initListeners()
    {
        mFramebodyMain_GridView_IndexMenu.setOnItemClickListener(this);
    }

    @Override
    protected void bindData()
    {
        mFramebodyMain_GridView_IndexMenu.setAdapter(mAdapterIndexMenu);
    }

    @Override
    protected void register()
    {
    }

    @Override
    protected Object doInBackground(Object... params)
    {
        initFields();
        return null;
    }

    @Override
    protected void onPostExecute(Object result)
    {
        super.onPostExecute(result);
        initViews();
        initListeners();
        bindData();
        register();
    }

}
